package com.dean.netty.client;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Scanner;

public class DeanNettyClientHandler extends SimpleChannelInboundHandler<String> {
    private final Logger logger = LoggerFactory.getLogger(DeanNettyClientHandler.class);
    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, String receiveMsg) throws Exception {
        // 通过ctx获取客户端的IP和端口号，并打印出客户端发来的消息
        logger.info("客户端接受到一条来自{}的消息,消息内容是:{}",channelHandlerContext.channel().remoteAddress(),receiveMsg);
        logger.info("请向服务端发送一条消息：");
        String sendMsg = new Scanner(System.in).nextLine();
        channelHandlerContext.channel().writeAndFlush(sendMsg);
    }

    public void channelActive(ChannelHandlerContext ctx) throws Exception{
        ctx.writeAndFlush("客户端发出第一条消息...");
    }
}
